//
//  HeadLineVC.m
//  Lucode
//
//  Created by lanou3g on 15/10/13.
//  Copyright © 2015年 liudi. All rights reserved.
//

#define kURL @"http://box.dwstatic.com/apiNewsList.php?action=l&newsTag=headlineNews&p=1"

#import "HeadLineVC.h"
#import "HeadLineCell.h"
#import "HeadLineModel.h"
#import "AfnHelper.h"
#import "SDCycleScrollView.h"
#import "UIImageView+WebCache.h"
#import "HeadLineDetailVC.h"

@interface HeadLineVC () <UITableViewDataSource, UITableViewDelegate, SDCycleScrollViewDelegate>
@property (nonatomic, strong) UITableView *headTableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView; // 轮播图
@property (nonatomic, strong) NSMutableArray *lbPicUrlArray; // 轮播图片链接数组
@property (nonatomic, strong) NSMutableArray *commentUrlArray;
@end
static NSString *biaozhi = @"headline";
@implementation HeadLineVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    
    [self.headTableView registerClass:[HeadLineCell class] forCellReuseIdentifier:biaozhi];
    self.headTableView.delegate = self;
    self.headTableView.dataSource = self;
    [self.view addSubview:self.headTableView];
    
    __weak typeof(self) temp = self;
    [AfnHelper getDataWithUrl:kURL dic:nil sucess:^(id opreation) {
        NSDictionary *dic = opreation;
        for (NSDictionary *ddd in [dic objectForKey:@"data"]) {
            HeadLineModel *model = [[HeadLineModel alloc] init];
                [model setValuesForKeysWithDictionary:ddd];
                [temp.dataArray addObject:model];
        }
        [temp.dataArray removeObjectAtIndex:0];
        for (NSDictionary *ddddd in [dic objectForKey:@"headerline"]) {
            HeadLineModel *model1 = [[HeadLineModel alloc] init];
            [model1 setValuesForKeysWithDictionary:ddddd];
            [temp.lbPicUrlArray addObject:model1.photo];
            [temp.commentUrlArray addObject:model1.commentUrl];
        }
        [temp.headTableView reloadData];
        [temp setLunboPic]; // 请求到数据之后添加轮播图
    } faile:^(id error) {
        NSLog(@"AfnHelper请求网络失败");
    }];
}
#pragma mark ----tableView的数据源及代理方法----
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HeadLineModel *hModel = self.dataArray[indexPath.row];
    HeadLineCell *cell = [tableView dequeueReusableCellWithIdentifier:biaozhi];
    cell.cellModel = hModel;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:hModel.photo]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 刚选中又马上取消选中，格子不变色
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HeadLineModel *hModel = self.dataArray[indexPath.row];
    NSRange range = {0, 17};
    NSString *str = [hModel.commentUrl substringWithRange:range]; // 截取需要拼接的字符串
    NSString *detailURLStr = [NSString stringWithFormat:@"http://lol.duowan.com/%@.html", str];
    HeadLineDetailVC *HLVC = [[HeadLineDetailVC alloc] init];
    UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:HLVC];
    HLVC.detailWebUrl = detailURLStr;
    [self.navigationController showViewController:navc sender:nil];
}

#pragma mark -------- 轮播图
- (void)setLunboPic {
    CGFloat width = self.view.bounds.size.width;
    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 320, width, 180) imageURLStringsGroup:self.lbPicUrlArray];
    _cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    _cycleScrollView.delegate = self;
    _cycleScrollView.autoScrollTimeInterval = 3;
    _cycleScrollView.dotColor = [UIColor yellowColor]; // 自定义分页控件小圆标颜色
    _cycleScrollView.placeholderImage = [UIImage imageNamed:@"placeholder"];
    self.headTableView.tableHeaderView = _cycleScrollView;
}
#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    NSString *commitUrlStr = self.commentUrlArray[index];
    NSRange range = {0, 17};
    NSString *str = [commitUrlStr substringWithRange:range]; // 截取需要拼接的字符串
    
    NSString *detailURLStr = [NSString stringWithFormat:@"http://lol.duowan.com/%@.html", str];
    HeadLineDetailVC *HLVC = [[HeadLineDetailVC alloc] init];
    UINavigationController *navc = [[UINavigationController alloc] initWithRootViewController:HLVC];
    HLVC.detailWebUrl = detailURLStr;
    [self.navigationController showViewController:navc sender:nil];
}

#pragma mark ----懒加载----
- (UITableView *)headTableView {
    if (!_headTableView) {
        _headTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-49-64-30) style:UITableViewStylePlain];
        _headTableView.backgroundColor = [UIColor clearColor];
    }
    return _headTableView;
}
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    } return _dataArray;
}
- (NSMutableArray *)lbPicUrlArray {
    if (!_lbPicUrlArray) {
        _lbPicUrlArray = [NSMutableArray array];
    } return _lbPicUrlArray;
}
- (NSMutableArray *)commentUrlArray {
    if (!_commentUrlArray) {
        _commentUrlArray = [NSMutableArray array];
    }
    return _commentUrlArray;
}

@end
